package com.summer.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetHtml {
	
	
	public static void main(String[] args) {
//		String url = "http://shishicai.cjcp.com.cn/";//"http://www.hnflcp.com/dwzdy_inc/dwzdy_kjlist.asp";//http://wsmajunfeng.iteye.com/blog/1629354
//		String url = "https://url.cn/5WKsCIQ";//"http://www.hnflcp.com/dwzdy_inc/dwzdy_kjlist.asp";//http://wsmajunfeng.iteye.com/blog/1629354
		String url = "http://162.250.141.13/bmkj.xml?s=3097.647452990977";//"http://www.hnflcp.com/dwzdy_inc/dwzdy_kjlist.asp";//http://wsmajunfeng.iteye.com/blog/1629354
		String html = GetHtml.getHtmlSource(url, 5*1000, "utf-8", "");
//		String html = GetHtml.getHtmlSource(url, 5*1000, "gbk", "");
		System.out.println(html);
		List<String> set = GetHtml.parseHtml(html);
		int index = 0;
		for(String str : set){
			System.out.print(str);
			if(index++>=7){
				System.out.println();
				index = 0;
			}
		}
		
//		List<String> setmp = GetHtml.parseHtmltmp(set.get(0));
//		for(String str : setmp){
//			System.out.println(str);
//		}
	}
	

	public static String getHtmlSource(String url, int Timeout, String encoding, String defaultValue) {
		HttpURLConnection connection = null;
		InputStreamReader reader = null;
		BufferedReader l_reader = null;
		try {
			if (encoding == null) {
				encoding = "gbk";
			}
			URL dataUrl = new URL(url);
			connection = (HttpURLConnection) dataUrl.openConnection();
			// 设置超时时间
			connection.setConnectTimeout(Timeout);
			connection.setReadTimeout(Timeout);
			//设置属性，如果不设置User-Agent可能会被某些网站当作爬虫过滤掉
			connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
			connection.connect();
//			for(List<String> list : connection.getHeaderFields().values()){
//				for(String s : list){
//
//					System.out.println("url=="+s);
//				}
//			}
			// InputStream l_urlStream = connection.getInputStream();
			reader = new InputStreamReader(connection.getInputStream(),encoding);
			// 读取请求结果
			// BufferedReader l_reader = new BufferedReader(new
			// InputStreamReader( connection.getInputStream(), encoding));
			l_reader = new BufferedReader(reader);

			String sCurrentLine = "";
			String sTotalString = "";
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString = sTotalString + sCurrentLine;
			}
			return sTotalString;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != l_reader) {
					l_reader.close();
				}
				if (null != reader) {
					reader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			connection.disconnect();
		}
		return defaultValue;
	}

	public static List<String> parseHtml(String htmlStr){
		

		List<String> pics = new ArrayList<>();
        Pattern pattern;
        Matcher matcher;
//        String regEx = "<font[^>].*?>(?<content>[^<>]+)</font>";
        String regEx = "<div class=\"kj-box clearfix\">*?</div>";
        pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(htmlStr);
        while (matcher.find()) {
        	
        	pics.add(matcher.group(0));
        	
            // 得到<img />数据
//            img = m_image.group(1);
//            // 匹配<img>中的src数据
////            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
////            Matcher m = Pattern.compile("<td( .*?)?>.*?</td>").matcher(img);
//            Matcher m = Pattern.compile("<font[^>].*?>(?<content>[^<>]+)</font>").matcher(img);
//            while (m.find()) {
//                pics.add(m.group(1));
//            }
        }
        
		
//		
//		Set<String> pics = new HashSet<>();
//        String img = "";
//        Pattern p_image;
//        Matcher m_image;
//        //     String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
////        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
//        String regEx = "<font[^>].*?>(?<content>[^<>]+)</font>";
//        p_image = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
//        m_image = p_image.matcher(htmlStr);
//        while (m_image.find()) {
//            // 得到<img />数据
//            img = m_image.group();
//            // 匹配<img>中的src数据
////            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
////            Matcher m = Pattern.compile("<td( .*?)?>.*?</td>").matcher(img);
//            Matcher m = Pattern.compile("<font[^>].*?>(?<content>[^<>]+)</font>").matcher(img);
//            while (m.find()) {
//                pics.add(m.group(1));
//            }
//        }
//        
        return pics;
	}

	public static List<String> parseHtmltmp(String htmlStr){
		
		List<String> list = new ArrayList<>();
        Pattern pattern;
        Matcher matcher;
        String regEx = "<li>.*?</li>";
        pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(htmlStr);
        while (matcher.find()) {
        	list.add(matcher.group(0));
        }
        return list;
	}
}
